var intervalID = 0;
var progressBar;
var fileUpload;
var form;

function pageLoad()
{
    //  attach to the upload click event and grab a reference to
    //  the progress bar
    $addHandler($get('upload'), 'click', onUploadClick);
    progressBar = $find('progress');    
}

function register(form, fileUpload) {
    //  register the form
    this.form = form;
    this.fileUpload = fileUpload;
}        

function onUploadClick() {
    //  todo: insert any regex to do any simple validation checks on
    //  the client.
	var regExp = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.xls|.XLS)$/;
    var valid = fileUpload.value.length > 0;
    
    if(!valid)
    {
        //  todo: insert what ever error message makes sense for
        //  the validation that was done.
        onComplete('error', g_str_checkEmpty);
    }
    else if(!regExp.test(fileUpload.value))
    {
        //  todo: insert what ever error message makes sense for
        //  the validation that was done.
        onComplete('error', g_str_checkExtensions);
    }
    else
    {
        //  disable the upload button
        $get('upload').disabled = 'disabled';
        
        //  update the message
        updateMessage('info', g_str_initializing);
        
        //  submit the form containing the fileupload
        form.submit();
        
        //  hide the frame
        Sys.UI.DomElement.addCssClass($get('uploadFrame'), 'hidden');
        
        //  zero out the progressbar and show it
        progressBar.set_percentage(0);
        progressBar.show();                
        
        //  start polling to check on the progress ...
        intervalID = window.setInterval(function()
        {
            PageMethods.GetUploadStatus(function(result)
            {
                if(result)
                {
                    //  update the progressbar to the new value
                    progressBar.set_percentage(result.percentComplete);
                    //  upadte the message
                    updateMessage('info', result.message);
                    
                    if(result == 100)
                    {
                        //  clear the interval
                        window.clearInterval(intervalID);                        
                    }
                }
            });
        }, 500); 
    }
}       

function onComplete(type, msg)
{
    //  clear the interval
    window.clearInterval(intervalID);
    //  display the message
    updateMessage(type, msg);
    //  hide the progress bar
    progressBar.hide();
    progressBar.set_percentage(0);
    //  re-enable the button
    $get('upload').disabled = '';
    //  show the frame
    Sys.UI.DomElement.removeCssClass($get('uploadFrame'), 'hidden');
    
    window.parent.window.opener.$get(window.parent.window.opener.g_str_btnUpload).click();
}

function updateMessage(type, value)
{
    var status = $get('status');
    status.innerHTML = value;
    //  remove all styles
    status.className = '';
    Sys.UI.DomElement.addCssClass(status, type);
}

function refresh()
{
	window.location.reload( false );
}